﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Ue.Tests
{
    /// <summary>
    /// Tests 的摘要说明
    /// </summary>
    [TestClass]
    public class UnitTests
    {
        [TestMethod]
        public void Tests()
        {
            var model1 = new FakeModel(() => 2);

            var serializeModel = SerializeObject(model1);
            var model2 = Deserialize(serializeModel);

            //Assert.AreEqual(model1, model2);
            Assert.AreEqual(2, model2.GetNumber());

        }

        public byte[] SerializeObject(FakeModel model)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(ms, model);
            ms.Position = 0;
            byte[] bytes = new byte[ms.Length];
            ms.Read(bytes, 0, bytes.Length); ms.Close(); return bytes;
        }

        public FakeModel Deserialize(byte[] model)
        {

            object obj = null;

            MemoryStream ms = new MemoryStream(model);
            ms.Position = 0;
            BinaryFormatter formatter = new BinaryFormatter();
            obj = formatter.Deserialize(ms);
            ms.Close();
            return obj as FakeModel;

        }
    }
    
    [Serializable]
    public class FakeModel
    {
        private Func<int> acqurieNumber;

        public FakeModel(Func<int> acqurieNumber)
        {
            this.acqurieNumber = acqurieNumber;
        }

        public int GetNumber()
        {
            return this.acqurieNumber();
        }
    }
}
